package cn.edu.buaa.nlsde.grid.Structure;

import java.util.ArrayList;
import java.util.List;

public class Grid
{
	public int gridID;
	public String gridName;
	private String authority;
	private String localTime;
	private int clusterNum;
	public List<Cluster> clusters;
	private String gridDesc;
	
	public Grid()
	{
		clusters=new ArrayList<Cluster>();
	}
	
	public Grid(List<Cluster> clusters)
	{
		this.clusters=new ArrayList<Cluster>();
		addClusters(clusters);
	}
	
	public int getID()
	{
		return gridID;
	}
	public void setID(int gridID)
	{
		this.gridID=gridID;
	}
	
	public String getName()
	{
		return gridName;
	}
	public void setName(String gridName)
	{
		this.gridName=gridName;
	}
	
	public String getAuthority()
	{
		return authority;
	}
	public void setAuthority(String authority)
	{
		this.authority=authority;
	}
	
	public String getLocalTime()
	{
		return localTime;
	}
	public void setLocalTime(String localTime)
	{
		this.localTime=localTime;
	}
	
	public List<Cluster> getClusters()
	{
		return this.clusters;
	}
	public void setClusters(List<Cluster> clusters)
	{
		this.clusters=clusters;
	}
	public Cluster getCluster(String clusterName)
	{
		Cluster cluster=null;
		List<Cluster> clusters=this.getClusters();
		for(int i=0;i<clusters.size();i++)
		{
			Cluster mycluster=(Cluster)clusters.get(i);
			if(mycluster.getName().equals(clusterName))
			{
				cluster=mycluster;
				break;
			}
		}
		return cluster;
	}
	
	public void addClusters(List<Cluster> clusters)
	{
		this.clusters.addAll(clusters);
	}
	public void addCluster(Cluster cluster)
	{
		this.clusters.add(cluster);
	}

	public int getClusterNum()
	{
		clusterNum=clusters.size();
		return clusterNum;
	}
	public void setClusterNum(int clusterNum)
	{
		this.clusterNum=clusterNum;
	}
	
	public List<Host> getHosts(String clusterName)
	{
		List<Cluster> clusters = this.getClusters();
		List<Host> hosts = new ArrayList<Host>();
		for(int i=0;i<clusters.size();i++)
		{
			Cluster cluster=(Cluster)clusters.get(i);
			if(clusterName.equals(((Cluster)clusters.get(i)).getName()))
			{
				hosts=(List<Host>)cluster.getHosts();
				break;
			}
		}
		return hosts;
	}
	public Host getHost(String clusterName,String hostName)
	{
		Host host=null;
		List<Host> hosts=this.getHosts(clusterName);
		for(int i=0;i<hosts.size();i++)
		{
			Host myhost=(Host)hosts.get(i);
			if(myhost.getName().equals(hostName))
			{
				host=myhost;
				break;
			}
		}
		return host;
	}
	
	public boolean hasCluster(String clusterName)
	{
		List<Cluster> clusters = this.getClusters();
		boolean hascluster = false;
		for(int i=0;i<clusters.size();i++)
		{
			Cluster cluster=(Cluster)clusters.get(i);
			if((cluster.getName()).equals(clusterName))
			{
				hascluster=true;
				break;
			}
		}
		return hascluster;
	}
	public boolean hasHost(String clusterName,String hostName)
	{
		List<Host> hosts=this.getHosts(clusterName);
		boolean hashost=false;
		for(int i=0;i<hosts.size();i++)
		{
			Host host=(Host)hosts.get(i);
			if((host.getName()).equals(hostName))
			{
				hashost=true;
				break;
			}
		}
		return hashost;
	}

	public String getGridDesc()
	{
		return gridDesc;
	}
	
	public void setGridDesc(String gridDesc)
	{
		this.gridDesc=gridDesc;
	}
}
